﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AskiNowclass;
using System.Data;
using System.Configuration;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Text;

public partial class video : System.Web.UI.Page
{
    
    protected string orderid = "";
    protected string userid = "";
    protected string touserid = "";
    protected string utype = "";//0学生1老师

    protected string studentid = "";
    protected string teacherid = "";

    protected string startdate = "";
    protected string subname = "";
    protected bool history_mode = false; 

    protected void Page_Load(object sender, EventArgs e)
    {
        WebHelper.PageNoCache();
        // These two booleans are quite similar
        // bool history_mode = false; 
        bool should_order_online = true;

        #region 缓存cookie判断是否存在
        userid=WebHelper.CookieReadOne("userid");

        if (userid == "") {

            WebHelper.Alert("Please login first");
            WebHelper.GoBack();
        }

        
        #endregion

        #region 订单id
        orderid = WebHelper.GetStringByFilter("orderid");

        if (orderid == "") {
            WebHelper.Alert("Invalid string of orderid");
            WebHelper.GoBack();
        }
        #endregion

        #region 是否需要“判断订单状态为50”的条件

        if (WebHelper.GetStringByFilter("should_order_online") == "false") {
            should_order_online = false;
        }
        #endregion

        #region 历史模式
        if (WebHelper.GetStringByFilter("history_mode") == "true") {
            history_mode = true;
        }
        #endregion
        
        #region 订单状态判断 必须为50
        try
        {
            #region 链接消息服务器，从服务器再判断
            string messageurl = ConfigurationManager.AppSettings["MessageUrl"].ToString();
            UTF8Encoding encoding = new UTF8Encoding();
            string restr = WebHelper.GetHtml(messageurl + "/api/orders/" + orderid + "", encoding);

           

            JObject json = JObject.Parse(restr);

            
             
            if (json["returnvalue"].ToString().Equals("True"))
            {
                JArray jsondata = (JArray)JsonConvert.DeserializeObject(json["data"].ToString());
                
                if (!should_order_online || jsondata[0]["orderState"].ToString().Trim().Equals("50"))
                {

                    
                    // 上课中
                    startdate = jsondata[0]["startTime"].ToString().Trim();
                    subname = jsondata[0]["subjectName"].ToString().Trim();
                    teacherid = jsondata[0]["teacherId"].ToString().Trim();
                    studentid = jsondata[0]["studentId"].ToString().Trim();
                    
                        if (teacherid.Equals(userid))
                        { 
                            touserid = studentid;
                            utype = "1"; 
                        }
                        else
                        { 
                            touserid = teacherid;
                            utype = "0"; 
                        }
                        
                }
                else
                { 
                    // 课程结束
                    WebHelper.Alert("Class is over, you can not visit through this link, please use class history.");
                    WebHelper.GoBack();
                }
            }
            else
            { 
                WebHelper.Alert(restr);
                WebHelper.GoBack();

            }

            #endregion

        }
        catch (Exception exception) {
            Response.Write(exception.Message);
            Response.End();
            
            // no need
            // WebHelper.Alert("message server error" + );
            WebHelper.GoBack();
        }         
        #endregion
    }

    
}